#include "MainFrame/CentralWidget/ImageTabWidget.h"

#include "MainFrame/CentralWidget/ImageViewWidget.h"

#include "IRIVIAProject.h"
#include "IRIVIATool.h"

ImageTabWidget::ImageTabWidget(QWidget *parent) :
    QTabWidget(parent),
    m_nCurWidgetID(0),
    m_pCurImageViewWidget(nullptr),
    m_nMaxID(1),
    m_pImageViewWidgetsMap(nullptr)
{
    m_pImageViewWidgetsMap = new QMap<int, ImageViewWidget*>;

    initWidget();
    connetSignalToSlots();
}

ImageTabWidget::~ImageTabWidget()
{
    delete m_pImageViewWidgetsMap;
    m_pImageViewWidgetsMap = nullptr;
}

void ImageTabWidget::initWidget()
{
    this->setTabsClosable(true);
}

void ImageTabWidget::connetSignalToSlots()
{
    connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(tabCloseSlot(int)));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChangeSlot(int)));
}

void ImageTabWidget::tabCloseSlot(int index)
{
    //remove from TabWidget
    this->removeTab(index);

    int nIDtoDel = -1;
    //remove from Map Table
    QMap<int, ImageViewWidget*>::iterator oIter = m_pImageViewWidgetsMap->begin() + index;
    if(oIter != m_pImageViewWidgetsMap->end())
    {
        ImageViewWidget* pFindedWidget = oIter.value();
        nIDtoDel = oIter.key();
        delete pFindedWidget;
        m_pImageViewWidgetsMap->remove(nIDtoDel);
    }

    //remove what in Entity module
    IRIVIAProject::getInstance()->deleteImageByID(nIDtoDel);

    emit imageDeleted(nIDtoDel);
}

void ImageTabWidget::tabChangeSlot(int index)
{
    if(index != -1)
    {
        QMap<int, ImageViewWidget*>::iterator oIter = m_pImageViewWidgetsMap->begin() + index;
        if(oIter != m_pImageViewWidgetsMap->end())
        {
            m_nCurWidgetID = oIter.key();
            m_pCurImageViewWidget = oIter.value();
        }
    }
    else
    {
        m_nCurWidgetID = 0;
        m_pCurImageViewWidget = nullptr;
    }

    IRIVIAProject::getInstance()->setCurImageID(m_nCurWidgetID);

    emit imageChanged(m_nCurWidgetID);
}

void ImageTabWidget::addImageViewWidget(const QString &imagename)
{
    int nID = m_nMaxID++;
    ImageViewWidget* pImageViewWidget = new ImageViewWidget(this);
    m_pImageViewWidgetsMap->insert(nID, pImageViewWidget);
    this->addTab(pImageViewWidget, imagename);
    this->setCurrentWidget(pImageViewWidget);

    connect(pImageViewWidget, SIGNAL(cursorPosChanged(QPoint&)),
            this, SIGNAL(cursorPosChanged(QPoint&)));

    m_nCurWidgetID = nID;
    m_pCurImageViewWidget = pImageViewWidget;

    pImageViewWidget->updateImageViewWidget();
    pImageViewWidget->setEnCurToolState(IRIVIATool().getEnCurTool());

    IRIVIAProject::getInstance()->setCurImageID(m_nCurWidgetID);

    emit imageAdded(nID, imagename);
}

void ImageTabWidget::updateImageViewWidget()
{
    QMap<int, ImageViewWidget*>::iterator oIter = m_pImageViewWidgetsMap->find(m_nCurWidgetID);
    if(oIter != m_pImageViewWidgetsMap->end())
        oIter.value()->updateImageViewWidget();
}

void ImageTabWidget::setEnCurToolState(const ToolState &enCurToolState)
{
    QMap<int, ImageViewWidget*>::iterator oIter = m_pImageViewWidgetsMap->begin();
    for(;oIter != m_pImageViewWidgetsMap->end(); ++oIter)
    {
        oIter.value()->setEnCurToolState(enCurToolState);
    }
}
